package com.sdry.web.controller.cb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.mapper.cb.StockMapper;
import com.sdry.model.cb.ReportFormEntity;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.lz.Customer;
import com.sdry.model.lz.Materiel;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.cb.ReportFormService;
import com.sdry.service.cb.StockService;
import com.sdry.service.lz.CustomerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 库存最新，上下表结构改造
 * @author cb
 * 2019年10月11日09:31:18
 */
@Controller
@RequestMapping("stock")
public class StockController extends BaseController{

    @Autowired
    private StockService stockService;

    @Autowired
    private ReportFormService reportFormService;
    @Resource
	CustomerService  customerService;
    @RequestMapping("page")
    public String index(Model model,HttpSession session,HttpServletResponse response){
		ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
		 if(staf.getRoleId().equals("20010,")) {
			 Customer customer=customerService.queryCustomerlxrById(staf.getCustomer_id());
			 model.addAttribute("cname", customer.getCustomer_name());
			 model.addAttribute("cid", staf.getCustomer_id());
			 model.addAttribute("lname", customer.getContacts_name());
			 List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
		        model.addAttribute("allCustomerList",allCustomerList);
		        return "bb/inventoryManagement7";
		 }else {
        //查询所有供应商
			 List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
		        model.addAttribute("allCustomerList",allCustomerList);
		        return "bb/inventoryManagement6";
		 }
    }

    /**
     * 列表上表
     * @param page 当前页
     * @param limit 大小
     * @param customerId 客户id
     * @param maNum QAD
     * @return
     */
    @RequestMapping("getStockList")
    @ResponseBody
    public TableDataInfo getStockList(
         Integer page ,Integer limit, Long customerId,String maNum
    ) {
        logger.info("获取库存下表条码表"+customerId+"= customerId" + maNum + "=customerId");
        PageHelper.startPage(page,limit);
        List<ZcInventoryInfoEntity> list = stockService.getStockList(customerId,maNum);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(),Math.toIntExact(pageInfo.getTotal()));
    }

    /**
     * 下表条码表
     * @param mid
     * @return
     */
    @RequestMapping("/getTiaoMaList")
    @ResponseBody
    public TableDataInfo getTiaoMaList(
            Integer page ,Integer pageSize,String mid
    ) {
        List<Materiel> list =  stockService.getTiaoMaList(page,pageSize,mid);
        Integer total =  stockService.getTiaoMaTotal(mid);
        return getDataTable(list,total);
    }

    @Autowired
    private StockMapper stockMapper;

    /**
     *
     */
    @RequestMapping("zhiXing")
    public void zhiXing() throws ParseException {
        String ma_id = "";
        String date = "2019-10-13";
        Long customer_id = null;
        String firsTime = "2019-09-01";
        String selectTime2="2019-10-13";
        List<ReportFormEntity> list1 = reportFormService.getReportFormList(ma_id,date,customer_id,firsTime,selectTime2);
        List<ZcInventoryInfoEntity> list2 = reportFormService.getReportInventoryList(customer_id,"","2019-10-13");
        for (int j = 0; j < list1.size(); j++) {
            Integer sum = 0;
            Long mid = 0L;
            for (int i = 0; i < list2.size(); i++) {

                if ( list1.get(j).getMaterielNum().equals(list2.get(i).getMateriel().getMateriel_num())) {

                    System.out.println("--------------------------------------");
                    System.out.println("list2.get(i).getmNum()"+list2.get(i).getmNum());
                    System.out.println("re.getLastMonthBalance()"+list1.get(j).getThisMonthBalance());
                    System.out.println("re.getMaterielNum()"+list1.get(j).getMaterielNum());
                    System.out.println("list2.get(i).getMateriel().getMateriel_num()"+list2.get(i).getMateriel().getMateriel_num());

                    sum =  Integer.valueOf(list2.get(i).getmNum()) - list1.get(j).getThisMonthBalance();

                    mid = list2.get(i).getMid();

                    System.out.println("sum"+sum);

                    System.out.println("--------------------------------------");
                }
            }
            Integer insert = stockMapper.insert(mid, sum);

        }

    }
}
